package com.hxkcloud.nurse.activitys;

import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.hxkcloud.nurse.R;
import com.hxkcloud.nurse.YjhApplication;
import com.hxkcloud.nurse.adapter.DetailListAdapter;
import com.hxkcloud.nurse.adapter.MonitorKindAdapter;
import com.hxkcloud.nurse.beans.LineListBean;
import com.hxkcloud.nurse.beans.MonitorKindBean;
import com.hxkcloud.nurse.beans.MonitorKindTestBean;
import com.hxkcloud.nurse.beans.PiontListBean;
import com.hxkcloud.nurse.beans.PointBean;
import com.hxkcloud.nurse.beans.resultbeans.BaseResultBean;
import com.hxkcloud.nurse.beans.resultbeans.ObjectResultBean;
import com.hxkcloud.nurse.dborm.MonitorKindDao;
import com.hxkcloud.nurse.request.MessageCode;
import com.hxkcloud.nurse.request.YiJiaHuNetWorkUtils;
//import com.yjhcloud.stroke.utils.BrightnessUtils;
import com.hxkcloud.nurse.utils.BrightnessUtils;
import com.hxkcloud.nurse.utils.StringUtil;
import com.hxkcloud.nurse.utils.Utils;
import com.hxkcloud.nurse.views.pulltorefreshlayout.PullToRefreshLayout;
import com.hxkcloud.nurse.views.pulltorefreshlayout.PullableRecyclerView;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import lecho.lib.hellocharts.listener.LineChartOnValueSelectListener;
import lecho.lib.hellocharts.model.Axis;
import lecho.lib.hellocharts.model.AxisValue;
import lecho.lib.hellocharts.model.Line;
import lecho.lib.hellocharts.model.LineChartData;
import lecho.lib.hellocharts.model.PointValue;
import lecho.lib.hellocharts.model.Viewport;
import lecho.lib.hellocharts.view.LineChartView;

/**
 * Created by ray_lei on 2016/8/5.
 */
public class ChartDetailActivity extends BaseActivity implements View.OnClickListener, DetailListAdapter.PostOptionsListener {

    private LinearLayout ll_title;
    private ImageView iv_back;
    private TextView tv_title;
    private TextView tv_input;
    private ImageView moreIV;

    private LinearLayout ivgLayout;
    private LinearLayout timeLayout;
    private LinearLayout catLayout;
    private TextView timeTV;//时间
    private TextView catTV;//类别
    private ImageView timeIV;
    private ImageView catIV;


    private LineChartView chart;
    private LineChartData data;


    private PullToRefreshLayout refreshLayout;
    private PullableRecyclerView recycleView;

    private List<PointBean> pointList;
    private DetailListAdapter adapter;

    private PopupWindow timePopup;//选择全部
    //    private Button dayBtn;
    private Button weekBtn;
    private Button monthBtn;
    private View timeWholeView;




    private PopupWindow kindPopup;//类别
    private ListView kindListView;
    private MonitorKindAdapter kindAdapter;

    private View kindWholeView;

    private String patientId;

    private String kindId;
    private String name;
    private String time;
    private String min;
    private String max;
    private String text1 = "";
    private String text2 = "";
    private String text3 = "";

    private TextView tv1;
    private TextView tv2;
    private TextView tv3;

    //    private TextView dayTV;
    private TextView weekTV;
    private TextView monthTV;
    private LinearLayout avgLayout;
    private LinearLayout xyLayout;
    private LinearLayout xy_enterings;
    private TextView xuya_high;
    private TextView xuya_low;




    private ObjectResultBean<LineListBean> resultBean;

    private String periodType = "1";

    private LinearLayout nodataLayout;

    private String testId = "";

    private TextView kindTV;
    private TextView timedisplayTV;

    private LinearLayout abnormalLayout;
    private LinearLayout lowLayout;
    private LinearLayout highLayout;

    String isKindBean = "";//1.血糖2.血压

    private ImageView nodataIV;
    private TextView nodataTV;

    private LinearLayout listtitleLayout;
    private LinearLayout ll_blood;
    private ImageView iconIV;

    private String monitorDate = "";

    private final String xuetang_default = "空腹";
    private final String xuezhi_default = "低密度";

    private PopupWindow popup_func;//更多
    private TextView tv_recovery;
    private View view_func1;
    private TextView tv_scale;
    private View view_func2;
    private TextView tv_question;

    private boolean isFromList = false;

    private String bloodMax = "";
    private String bloodMin = "";
    private String axMin = "";



    @Override
    protected void updateByHandler(Message msg) {
        switch (msg.what) {
            case MessageCode.NETWORKCODE_GETMONITORDATA_SUCCESS:
                dismisssLoading();
                if (msg.obj != null) {
                    resultBean = (ObjectResultBean<LineListBean>) msg.obj;
                    Log.i("-----resultBean----->",resultBean.getData().toString());
                  //  bloodMax = resultBean.getData().getBoolList();
                    if (resultBean.getData().getBoolList().get(0).getBloodMax()!=null){
                        bloodMax = resultBean.getData().getBoolList().get(0).getBloodMax();
                    }else {
                        bloodMax = resultBean.getData().getBoolList().get(0).getAxMax();
                    }
                    if (resultBean.getData().getBoolList().get(0).getBloodMin()!=null){
                        bloodMin = resultBean.getData().getBoolList().get(0).getBloodMin();
                    }else {
                        bloodMin = resultBean.getData().getBoolList().get(0).getInMax();
                    }
                    axMin = resultBean.getData().getBoolList().get(0).getAxMin();
                    xuya_high.setText(bloodMax);
                    xuya_low.setText(bloodMin);

                    if (resultBean.getData().getLineList() != null) {
                        if ("0".equals(periodType)) {
                            generateData(min, max, resultBean.getData().getLineList(), chart);
                        } else if ("1".equals(periodType)) {
                            generateData1(min, max, resultBean.getData().getLineList(), chart);
                        } else if ("2".equals(periodType)) {
                            generateData2(min, max, resultBean.getData().getLineList(), chart);
                        }
                        if (resultBean.getData().getLineList().get(0).getPointList().size() == 0) {
                            nodataLayout.setVisibility(View.VISIBLE);
                            nodataIV.setVisibility(View.VISIBLE);
                            nodataTV.setVisibility(View.VISIBLE);
                            if ("1".equals(isKindBean)) {
                                nodataIV.setImageResource(R.mipmap.monitor_no_data_purpli);
                                nodataTV.setTextColor(mContext.getResources().getColor(R.color.purpli));
                            } else if ("2".equals(isKindBean)) {
                                nodataIV.setImageResource(R.mipmap.monitor_no_data);
                                nodataTV.setTextColor(mContext.getResources().getColor(R.color.blue_text));
                            } else {
                                nodataIV.setImageResource(R.mipmap.monitor_no_data);
                                nodataTV.setTextColor(mContext.getResources().getColor(R.color.blue_text));
                            }
                        } else {
                            nodataLayout.setVisibility(View.GONE);
                        }
//                        Log.i("--------->","resultBean:"+resultBean.getData().getLineList().size());
                        for (int i = 0; i < resultBean.getData().getLineList().size(); i++) {
                            if (i == 0) {
                                pointList = resultBean.getData().getLineList().get(0).getPointList();
                                tv1.setText(resultBean.getData().getLineList().get(0).getName());
                            } else if (i == 1) {
                                tv2.setText(resultBean.getData().getLineList().get(1).getName());
                                if (pointList != null) {
                                    for (int j = 0; j < pointList.size(); j++) {
//                                        Log.i("--------->","pointList:"+pointList.size());
                                        pointList.get(j).setValue1(resultBean.getData().getLineList().get(1).getPointList().get(j).getValue());
                                        pointList.get(j).setPointId1(resultBean.getData().getLineList().get(1).getPointList().get(j).getPointId());
                                        pointList.get(j).setIsSpecial1(resultBean.getData().getLineList().get(1).getPointList().get(j).getIsSpecial());
                                    }
                                }
                            } else if (i == 2) {
                                tv3.setText(resultBean.getData().getLineList().get(2).getName());
                                if (pointList != null) {
                                    for (int j = 0; j < pointList.size(); j++) {
                                        pointList.get(j).setValue2(resultBean.getData().getLineList().get(2).getPointList().get(j).getValue());
                                        pointList.get(j).setPointId2(resultBean.getData().getLineList().get(2).getPointList().get(j).getPointId());
                                        pointList.get(j).setIsSpecial2(resultBean.getData().getLineList().get(2).getPointList().get(j).getIsSpecial());
                                    }
                                }
                            }
                        }

                        adapter.setData(pointList);


                    }
                }

                break;
            case MessageCode.NETWORKCODE_GETMONITORDATA_FAILURE:
                dismisssLoading();
                if (msg.obj != null) {
                    toastMsg(msg.obj.toString());
                    return;
                }

                break;
            case MessageCode.NETWORKCODE_GETMONITORDELETE_SUCCESS:
                BaseResultBean bean = (BaseResultBean) msg.obj;
                if (bean != null) {
                    toastMsg(bean.getMessage());
                    loading();
                    YiJiaHuNetWorkUtils.getMonitorData(patientId, periodType, kindId, testId, new Date().getTime() + "", handler);
                }
                break;

            case MessageCode.NETWORKCODE_GETMONITORDELETE_FAILURE:
                dismisssLoading();
                if (msg.obj != null) {
                    toastMsg(msg.obj.toString());
                    return;
                }
                break;

        }

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_layout_xuetangdetail);
        if(getIntent().hasExtra("isFromList")){
            isFromList = getIntent().getBooleanExtra("isFromList",false);
        }
        pointList = new ArrayList<PointBean>();
        tv1 = (TextView) findViewById(R.id.chartdetail_tv_text1);
        tv2 = (TextView) findViewById(R.id.chartdetail_tv_text2);
        tv3 = (TextView) findViewById(R.id.chartdetail_tv_text3);
        chart = (LineChartView) findViewById(R.id.xuetangdetail_chart);

        ll_title = (LinearLayout) findViewById(R.id.ll_title);
        iv_back = (ImageView) findViewById(R.id.iv_title_back);
        tv_title = (TextView) findViewById(R.id.tv_title_title);
        tv_input = (TextView) findViewById(R.id.tv_title_func);
        moreIV = (ImageView) findViewById(R.id.iv_title_func);

        setTitlePadding(ll_title);
        iv_back.setVisibility(View.VISIBLE);
        iv_back.setOnClickListener(this);
        tv_input.setText(R.string.monitor_text_btn_input);
        tv_input.setOnClickListener(this);
        moreIV.setOnClickListener(this);
        if(loginInfo.getUserStyle().equals(YjhApplication.userstyle_nurse)){
            if(isFromList){
                tv_input.setVisibility(View.INVISIBLE);
                moreIV.setVisibility(View.GONE);
            }else{
                tv_input.setVisibility(View.INVISIBLE);
                moreIV.setVisibility(View.VISIBLE);
                moreIV.setImageResource(R.mipmap.chartdetail_more);
            }
        }else if(loginInfo.getUserStyle().equals(YjhApplication.userstyle_doctor)){
            moreIV.setVisibility(View.GONE);
            tv_input.setVisibility(View.INVISIBLE);
        }else if(loginInfo.getUserStyle().equals(YjhApplication.userstyle_patient)){
            moreIV.setVisibility(View.GONE);
            tv_input.setVisibility(View.VISIBLE);
        }

        refreshLayout = (PullToRefreshLayout) findViewById(R.id.xuetangdetail_refreshlayout);
        recycleView = (PullableRecyclerView) findViewById(R.id.xuetangdetail_recyclerview);

        LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
        mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycleView.setLayoutManager(mLayoutManager);
//        ListDividerItemDecoration itemDecoration = new ListDividerItemDecoration(
//                this, ListDividerItemDecoration.VERTICAL_LIST);
//        recycleView.addItemDecoration(itemDecoration);
        recycleView.setCanRefresh(false);
        recycleView.setCanLoadMore(false);

        adapter = new DetailListAdapter(this, pointList, recycleView, loginInfo.getUserStyle());
        adapter.setPostOptionsListener(this);
        recycleView.setAdapter(adapter);

        nodataLayout = (LinearLayout) findViewById(R.id.chartdetail_layout_nodata);

        kindTV = (TextView) findViewById(R.id.xuetangdetail_tv_kindtv);
        timedisplayTV = (TextView) findViewById(R.id.xuetangdetail_tv_timetv);
        iconIV = (ImageView) findViewById(R.id.xuetangdetail_iv_kindiv);

//        RelativeLayout.LayoutParams lp_chart=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,screenWidth*3/5);
//        lp_chart.addRule(RelativeLayout.BELOW,R.id.xuetangdetail_layout_charttitle);
//        lp_chart.leftMargin= ImageUtil.dip2px(5, mContext);
//        chart.setLayoutParams(lp_chart);

        initView();
        initPopup();
        initData();
        initListener();


    }

    @Override
    protected void initView() {
        ivgLayout = (LinearLayout) findViewById(R.id.xuetangdetail_layout_ivg);
        ll_blood = (LinearLayout) findViewById(R.id.ll_linear_blood);
        timeLayout = (LinearLayout) findViewById(R.id.xuetangdetail_layout_time);
        catLayout = (LinearLayout) findViewById(R.id.xuetangdetail_layout_cat);
        timeTV = (TextView) findViewById(R.id.xuetangdetail_tv_time);
        catTV = (TextView) findViewById(R.id.xuetangdetail_tv_cat);
        timeIV = (ImageView) findViewById(R.id.xuetangdetail_iv_time);
        catIV = (ImageView) findViewById(R.id.xuetangdetail_iv_cat);

//        dayTV = (TextView) findViewById(R.id.xueyadetail_tv_day);
        weekTV = (TextView) findViewById(R.id.xueyadetail_tv_week);
        monthTV = (TextView) findViewById(R.id.xueyadetail_tv_month);
        avgLayout = (LinearLayout) findViewById(R.id.xueyadetail_layout_avg);
        xyLayout = (LinearLayout) findViewById(R.id.xueya_entering);
        xy_enterings = (LinearLayout) findViewById(R.id.xuetang_settings);
        xuya_high = (TextView) findViewById(R.id.xuya_hight);
        xuya_low = (TextView) findViewById(R.id.xuya_low);


        abnormalLayout = (LinearLayout) findViewById(R.id.xueyadetail_layout_abnormal);
        lowLayout = (LinearLayout) findViewById(R.id.xueyadetail_layout_low);
        highLayout = (LinearLayout) findViewById(R.id.xueyadetail_layout_high);

        nodataIV = (ImageView) findViewById(R.id.chartdetail_iv_nodata);
        nodataTV = (TextView) findViewById(R.id.chartdetail_tv_nodata);

        listtitleLayout = (LinearLayout) findViewById(R.id.xueyadetail_layout_listtitle);
    }

    private void initPopup() {
        popup_func = new PopupWindow(this);
        View viewFunc = LayoutInflater.from(this).inflate(R.layout.popup_chartinfo_func, null);
        popup_func.setContentView(viewFunc);

        popup_func.setWidth(screenWidth / 3);
        popup_func.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        popup_func.setFocusable(true);
        popup_func.setTouchable(true);
        popup_func.setOutsideTouchable(true);
        popup_func.setBackgroundDrawable(new ColorDrawable(0x00000000));
        popup_func.setAnimationStyle(R.style.popupwindow_anim_alpha);

        tv_recovery = (TextView) viewFunc.findViewById(R.id.tv_chartinfo_func1);
        tv_scale = (TextView) viewFunc.findViewById(R.id.tv_chartinfo_func2);
        tv_question = (TextView) viewFunc.findViewById(R.id.tv_chartinfo_func3);
        view_func1 = viewFunc.findViewById(R.id.view_chartinfo_func1);
        view_func2 = viewFunc.findViewById(R.id.view_chartinfo_func2);
        tv_recovery.setOnClickListener(this);
        tv_scale.setOnClickListener(this);
        tv_question.setOnClickListener(this);

        popup_func.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                BrightnessUtils.lightOn(ChartDetailActivity.this);
            }
        });

    }

    @Override
    protected void initListener() {
        ll_blood.setOnClickListener(this);
        timeLayout.setOnClickListener(this);
        catLayout.setOnClickListener(this);
        kindWholeView.setOnClickListener(this);
        timeWholeView.setOnClickListener(this);
//        dayTV.setOnClickListener(this);
        weekTV.setOnClickListener(this);
        monthTV.setOnClickListener(this);
        xy_enterings.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        if (getIntent().hasExtra("patientId")) {
            patientId = getIntent().getStringExtra("patientId");
        }
        if (getIntent().hasExtra("kindId")) {
            kindId = getIntent().getStringExtra("kindId");
        }
        if (getIntent().hasExtra("name")) {
            name = getIntent().getStringExtra("name");
        }
        tv_title.setText(name);
        kindTV.setText(name);
        if (getIntent().hasExtra("date")) {
            monitorDate = getIntent().getStringExtra("date");
        }
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String now = sdf.format(date);
        timedisplayTV.setText(now);
        if (getIntent().hasExtra("min")) {
            min = getIntent().getStringExtra("min");
        }
        if (getIntent().hasExtra("max")) {
            max = getIntent().getStringExtra("max");
        }
        if (getIntent().hasExtra("text0")) {
            text1 = getIntent().getStringExtra("text0");
            tv1.setVisibility(View.VISIBLE);
//            tv1.setText(text1);
        }
        if (getIntent().hasExtra("text1")) {
            text2 = getIntent().getStringExtra("text1");
            tv2.setVisibility(View.VISIBLE);
//            tv2.setText(text2);
        }
        if (getIntent().hasExtra("text2")) {
            text3 = getIntent().getStringExtra("text2");
            tv3.setVisibility(View.VISIBLE);
//            tv3.setText(text3);
        }

//        if (bloodMax!=null&&bloodMax!=""){
//            xuya_high.setText(bloodMax);
//        }
//        if (bloodMin!=null&&bloodMin!=""){
//            xuya_low.setText(bloodMin);
//        }

        MonitorKindDao dao = new MonitorKindDao(mContext);
        List<MonitorKindBean> kinds = dao.getKinds();
        for (int i = 0; i < kinds.size(); i++) {
            if (kindId.equals(kinds.get(i).getKindId())) {
                if (kinds.get(i).getTestList().size() == 0) {
                    ivgLayout.setVisibility(View.GONE);
                    avgLayout.setVisibility(View.VISIBLE);
                    xyLayout.setVisibility(View.VISIBLE);
                    abnormalLayout.setVisibility(View.VISIBLE);
                    lowLayout.setVisibility(View.VISIBLE);
                    highLayout.setVisibility(View.VISIBLE);
                    isKindBean = "2";
                    listtitleLayout.setBackground(mContext.getResources().getDrawable(R.drawable.bg_grey_corner));
                    iconIV.setImageResource(R.mipmap.monitor_pressure);
                    break;
                } else {
                    ivgLayout.setVisibility(View.VISIBLE);
                    avgLayout.setVisibility(View.GONE);
                    xyLayout.setVisibility(View.GONE);
                    abnormalLayout.setVisibility(View.VISIBLE);
                    lowLayout.setVisibility(View.GONE);
                    highLayout.setVisibility(View.GONE);
                    isKindBean = "1";
                    listtitleLayout.setBackground(mContext.getResources().getDrawable(R.drawable.bg_purpli_corner));
                    iconIV.setImageResource(R.mipmap.monitor_blood);
                    break;
                }
            }
        }

        initTimePopup();
        initKindPopup();

    }

    @Override
    public void onResume() {
        super.onResume();

        loading();
        if (!"".equals(monitorDate)) {
            YiJiaHuNetWorkUtils.getMonitorData(patientId, periodType, kindId, testId, monitorDate, handler);
        } else {
            YiJiaHuNetWorkUtils.getMonitorData(patientId, periodType, kindId, testId, new Date().getTime() + "", handler);
        }
    }

    private void generateData(String min, String max, List<PiontListBean> pointList, LineChartView chart) {
        List<Line> lines = new ArrayList<Line>();
        if (pointList != null) {

            for (int i = 0; i < pointList.size(); ++i) {
                List<PointValue> values = new ArrayList<PointValue>();
                if (pointList.get(i).getPointList() != null) {
                    for (int j = 0; j < pointList.get(i).getPointList().size(); j++) {
                        PointBean bean = pointList.get(i).getPointList().get(j);
                        long timeStamp = Long.parseLong(bean.getTime());
                        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        Date d1 = new Date(timeStamp);
                        int timepoint = d1.getHours();
                        PointValue point = new PointValue(timepoint, (int) Double.parseDouble(bean.getValue()));
                        Resources res = mContext.getResources();

                        if ("1".equals(isKindBean)) {
                            point.setColor("0");//紫色
                            point.setPurpliColor(mContext.getResources().getColor(R.color.purpli));
                            if ("0".equals(bean.getIsSpecial())) {
                                Bitmap bmp = BitmapFactory.decodeResource(res, R.mipmap.monitor_purple_circle);
                                point.setBitmap(bmp);
                            } else {
                                point.setBig(true);
                                Bitmap bmp = BitmapFactory.decodeResource(res, R.mipmap.monitor_red_circle);
                                point.setBitmap(bmp);
                            }

                        } else if ("2".equals(isKindBean)) {
                            if (mContext.getResources().getString(R.string.high_value).equals(pointList.get(i).getName())) {
                                point.setColor("1");//深蓝
                                Bitmap bmp = BitmapFactory.decodeResource(res, R.mipmap.monitor_blue_circle);
                                point.setBitmap(bmp);
                            } else if (mContext.getResources().getString(R.string.low_value).equals(pointList.get(i).getName())) {
                                point.setColor("2");//黄色
                                Bitmap bmp = BitmapFactory.decodeResource(res, R.mipmap.monitor_yellow_circle);
                                point.setBitmap(bmp);
                            }
                            if ("0".equals(bean.getIsSpecial())) {
                                if (mContext.getResources().getString(R.string.high_value).equals(pointList.get(i).getName())) {
                                    Bitmap bmp = BitmapFactory.decodeResource(res, R.mipmap.monitor_blue_circle);
                                    point.setBitmap(bmp);
                                } else if (mContext.getResources().getString(R.string.low_value).equals(pointList.get(i).getName())) {
                                    Bitmap bmp = BitmapFactory.decodeResource(res, R.mipmap.monitor_yellow_circle);
                                    point.setBitmap(bmp);
                                }
                            } else {
                                point.setBig(true);
                                Bitmap bmp = BitmapFactory.decodeResource(res, R.mipmap.monitor_red_circle);
                                point.setBitmap(bmp);
                            }
                        } else {
                            if ("0".equals(bean.getIsSpecial())) {
                                Bitmap bmp = BitmapFactory.decodeResource(res, R.mipmap.monitor_purple_circle);
                                point.setBitmap(bmp);
                            } else {
                                point.setBig(true);
                                Bitmap bmp = BitmapFactory.decodeResource(res, R.mipmap.monitor_red_circle);
                                point.setBitmap(bmp);
                            }
                        }
                        values.add(point);
                    }
                    Line line = new Line(values);
                    if ("1".equals(isKindBean)) {
                        line.setColor(mContext.getResources().getColor(R.color.purpli));
                    } else if ("2".equals(isKindBean)) {
                        if (mContext.getResources().getString(R.string.high_value).equals(pointList.get(i).getName())) {
                            line.setColor(mContext.getResources().getColor(R.color.blue));
                        } else if (mContext.getResources().getString(R.string.low_value).equals(pointList.get(i).getName())) {
                            line.setColor(mContext.getResources().getColor(R.color.possible_result_points));
                        }
                    } else {
                        line.setColor(mContext.getResources().getColor(R.color.blue));
                    }

                    lines.add(line);

                }
            }

            data = new LineChartData(lines);

            List<AxisValue> mAxisValues = new ArrayList<AxisValue>();
            mAxisValues.add(new AxisValue(0).setLabel("00:00")); //为每个对应的i设置相应的label(显示在X轴)
            mAxisValues.add(new AxisValue(1).setLabel("01:00"));
            mAxisValues.add(new AxisValue(2).setLabel("02:00"));
            mAxisValues.add(new AxisValue(3).setLabel("03:00"));
            mAxisValues.add(new AxisValue(4).setLabel("04:00"));
            mAxisValues.add(new AxisValue(5).setLabel("05:00"));
            mAxisValues.add(new AxisValue(6).setLabel("06:00"));
            mAxisValues.add(new AxisValue(7).setLabel("07:00"));
            mAxisValues.add(new AxisValue(8).setLabel("08:00"));
            mAxisValues.add(new AxisValue(9).setLabel("09:00"));
            mAxisValues.add(new AxisValue(10).setLabel("10:00"));
            mAxisValues.add(new AxisValue(11).setLabel("11:00"));
            mAxisValues.add(new AxisValue(12).setLabel("12:00"));
            mAxisValues.add(new AxisValue(13).setLabel("13:00"));
            mAxisValues.add(new AxisValue(14).setLabel("14:00"));
            mAxisValues.add(new AxisValue(15).setLabel("15:00"));
            mAxisValues.add(new AxisValue(16).setLabel("16:00"));
            mAxisValues.add(new AxisValue(17).setLabel("17:00"));
            mAxisValues.add(new AxisValue(18).setLabel("18:00"));
            mAxisValues.add(new AxisValue(19).setLabel("19:00"));
            mAxisValues.add(new AxisValue(20).setLabel("20:00"));
            mAxisValues.add(new AxisValue(21).setLabel("21:00"));
            mAxisValues.add(new AxisValue(22).setLabel("22:00"));
            mAxisValues.add(new AxisValue(23).setLabel("23:00"));
            mAxisValues.add(new AxisValue(24).setLabel("24:00"));


            Axis axisX = new Axis(mAxisValues);

            List<AxisValue> yAxisValues = new ArrayList<AxisValue>();
            int maxInt = (int) Double.parseDouble(max);
            int minInt = (int) Double.parseDouble(min);
//            int interValue = (maxInt - minInt) / 5;
            int interValue = (int) Math.ceil(((double) (maxInt - minInt) / 5));
            yAxisValues.add(new AxisValue(interValue).setLabel(interValue + "")); //为每个对应的i设置相应的label(显示在X轴)
            yAxisValues.add(new AxisValue(interValue * 2).setLabel((interValue * 2) + ""));
            yAxisValues.add(new AxisValue(interValue * 3).setLabel((interValue * 3) + ""));
            yAxisValues.add(new AxisValue(interValue * 4).setLabel((interValue * 4) + ""));
            yAxisValues.add(new AxisValue(interValue * 5).setLabel((interValue * 5) + ""));

            Axis axisY = new Axis(yAxisValues).setHasLines(true);

            data.setAxisXBottom(axisX);
            data.setAxisYLeft(axisY);

            chart.setLineChartData(data);

            final Viewport v = new Viewport(chart.getMaximumViewport());
            v.bottom = 0;
            v.top = (int) Double.parseDouble(max) + maxInt / 10;
            v.left = 0;
            v.right = 30 - 1;
            chart.setMaximumViewport(v);
            chart.setCurrentViewport(v);

        }
    }

    private void generateData1(String min, String max, List<PiontListBean> pointList, LineChartView chart) {
        List<Line> lines = new ArrayList<Line>();
        if (pointList != null) {

            for (int i = 0; i < pointList.size(); ++i) {
                List<PointValue> values = new ArrayList<PointValue>();
                if (pointList.get(i).getPointList() != null) {
                    for (int j = 0; j < pointList.get(i).getPointList().size(); j++) {
                        PointBean bean = pointList.get(i).getPointList().get(j);
                        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        long timeStamp = Long.parseLong(bean.getTime());
                        String pointTime = Utils.timeStamp2Date((timeStamp) + "", "yyyy-MM-dd HH:mm:ss");
                        String currentTime = format.format(new java.util.Date());
                        int days = 0;
                        try {
                            days = daysBetween(pointTime, currentTime);
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                        Date d1 = new Date(timeStamp);
                        int timepoint = ((7 - days) * 24) + d1.getHours();
                        PointValue point = new PointValue(timepoint, (int) Double.parseDouble(bean.getValue()));

                        Resources res = mContext.getResources();
                        if ("1".equals(isKindBean)) {
                            point.setColor("0");//紫色
                            point.setPurpliColor(mContext.getResources().getColor(R.color.purpli));
                            if ("0".equals(bean.getIsSpecial())) {
                                Bitmap bmp = BitmapFactory.decodeResource(res, R.mipmap.monitor_purple_circle);
                                point.setBitmap(bmp);
                            } else {
                                point.setBig(true);
                                Bitmap bmp = BitmapFactory.decodeResource(res, R.mipmap.monitor_red_circle);
                                point.setBitmap(bmp);
                            }

                        } else if ("2".equals(isKindBean)) {
                            if (mContext.getResources().getString(R.string.high_value).equals(pointList.get(i).getName())) {
                                point.setColor("1");//深蓝
                                Bitmap bmp = BitmapFactory.decodeResource(res, R.mipmap.monitor_blue_circle);
                                point.setBitmap(bmp);
                            } else if (mContext.getResources().getString(R.string.low_value).equals(pointList.get(i).getName())) {
                                point.setColor("2");//黄色
                                Bitmap bmp = BitmapFactory.decodeResource(res, R.mipmap.monitor_yellow_circle);
                                point.setBitmap(bmp);
                            }
                            if ("0".equals(bean.getIsSpecial())) {
                                if (mContext.getResources().getString(R.string.high_value).equals(pointList.get(i).getName())) {
                                    Bitmap bmp = BitmapFactory.decodeResource(res, R.mipmap.monitor_blue_circle);
                                    point.setBitmap(bmp);
                                } else if (mContext.getResources().getString(R.string.low_value).equals(pointList.get(i).getName())) {
                                    Bitmap bmp = BitmapFactory.decodeResource(res, R.mipmap.monitor_yellow_circle);
                                    point.setBitmap(bmp);
                                }
                            } else {
                                point.setBig(true);
                                Bitmap bmp = BitmapFactory.decodeResource(res, R.mipmap.monitor_red_circle);
                                point.setBitmap(bmp);
                            }
                        } else {
                            if ("0".equals(bean.getIsSpecial())) {
                                Bitmap bmp = BitmapFactory.decodeResource(res, R.mipmap.monitor_purple_circle);
                                point.setBitmap(bmp);
                            } else {
                                point.setBig(true);
                                Bitmap bmp = BitmapFactory.decodeResource(res, R.mipmap.monitor_red_circle);
                                point.setBitmap(bmp);
                            }
                        }
                        values.add(point);
                    }
                    Line line = new Line(values);
                    if ("1".equals(isKindBean)) {
                        line.setColor(mContext.getResources().getColor(R.color.purpli));
                    } else if ("2".equals(isKindBean)) {
                        if (mContext.getResources().getString(R.string.high_value).equals(pointList.get(i).getName())) {
                            line.setColor(mContext.getResources().getColor(R.color.blue));
                        } else if (mContext.getResources().getString(R.string.low_value).equals(pointList.get(i).getName())) {
                            line.setColor(mContext.getResources().getColor(R.color.possible_result_points));
                        }
                    } else {
                        line.setColor(mContext.getResources().getColor(R.color.blue));
                    }

                    lines.add(line);


                }
            }

            data = new LineChartData(lines);

            List<AxisValue> mAxisValues = new ArrayList<AxisValue>();
            for (int i = 0; i < 168; i++) {
                String newDate = getDateAdd(7 - (i / 24));
                mAxisValues.add(new AxisValue(i).setLabel(newDate.split("-")[1] + "-" + newDate.split("-")[2]));
//                for (int j = 0; j < 7; j++) {
//                    if (i >= (j * 24) && i < ((j + 1) * 24)) {
//                        if (j == 0) {
//                            mAxisValues.add(new AxisValue(i).setLabel(""));
//                            break;
//                        }
//                        if (j % 2 == 0) {
//                            if (i >= 140 && i < 160) {
//                                mAxisValues.add(new AxisValue(i).setLabel(""));
//                                break;
//                            }
//                            String newDate = getDateAdd(7 - j);
//                            mAxisValues.add(new AxisValue(i).setLabel(newDate.split("-")[1] + "-" + newDate.split("-")[2]));
//                            break;
//                        } else {
//                            mAxisValues.add(new AxisValue(i).setLabel(""));
//                            break;
//                        }
//
//                    }
//                }
            }

            Axis axisX = new Axis(mAxisValues);

            List<AxisValue> yAxisValues = new ArrayList<AxisValue>();
            int maxInt = (int) Double.parseDouble(max);
            int minInt = (int) Double.parseDouble(min);
//            int interValue = (maxInt - minInt) / 5;
            int interValue = (int) Math.ceil(((double) (maxInt - minInt) / 5));
            yAxisValues.add(new AxisValue(interValue).setLabel(interValue + "")); //为每个对应的i设置相应的label(显示在X轴)
            yAxisValues.add(new AxisValue(interValue * 2).setLabel((interValue * 2) + ""));
            yAxisValues.add(new AxisValue(interValue * 3).setLabel((interValue * 3) + ""));
            yAxisValues.add(new AxisValue(interValue * 4).setLabel((interValue * 4) + ""));
            yAxisValues.add(new AxisValue(interValue * 5).setLabel((interValue * 5) + ""));

            Axis axisY = new Axis(yAxisValues).setHasLines(true);

            data.setAxisXBottom(axisX);
            data.setAxisYLeft(axisY);

            chart.setLineChartData(data);

            final Viewport v = new Viewport(chart.getMaximumViewport());
            v.bottom = 0;
            v.top = (int) Double.parseDouble(max) + maxInt / 10;
            v.left = 0;
            v.right = 200;
            chart.setMaximumViewport(v);
            chart.setCurrentViewport(v);

        }
    }

    private void generateData2(String min, String max, List<PiontListBean> pointList, LineChartView chart) {
        List<Line> lines = new ArrayList<Line>();
        if (pointList != null) {

            for (int i = 0; i < pointList.size(); ++i) {
                List<PointValue> values = new ArrayList<PointValue>();
                if (pointList.get(i).getPointList() != null) {
                    for (int j = 0; j < pointList.get(i).getPointList().size(); j++) {
                        PointBean bean = pointList.get(i).getPointList().get(j);
                        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        long timeStamp = Long.parseLong(bean.getTime());
                        String pointTime = Utils.timeStamp2Date((timeStamp) + "", "yyyy-MM-dd HH:mm:ss");
                        String currentTime = format.format(new java.util.Date());
                        int days = 0;
                        try {
                            days = daysBetween(pointTime, currentTime);
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                        Date d1 = new Date(timeStamp);
                        int timepoint = ((30 - days) * 24) + d1.getHours();
                        PointValue point = new PointValue(timepoint, (int) Double.parseDouble(bean.getValue()));
                        Resources res = mContext.getResources();

                        if ("1".equals(isKindBean)) {
                            point.setColor("0");//紫色
                            point.setPurpliColor(mContext.getResources().getColor(R.color.purpli));
                            if ("0".equals(bean.getIsSpecial())) {
                                Bitmap bmp = BitmapFactory.decodeResource(res, R.mipmap.monitor_purple_circle);
                                point.setBitmap(bmp);
                            } else {
                                point.setBig(true);
                                Bitmap bmp = BitmapFactory.decodeResource(res, R.mipmap.monitor_red_circle);
                                point.setBitmap(bmp);
                            }

                        } else if ("2".equals(isKindBean)) {
                            if (mContext.getResources().getString(R.string.high_value).equals(pointList.get(i).getName())) {
                                point.setColor("1");//深蓝
                                Bitmap bmp = BitmapFactory.decodeResource(res, R.mipmap.monitor_blue_circle);
                                point.setBitmap(bmp);
                            } else if (mContext.getResources().getString(R.string.low_value).equals(pointList.get(i).getName())) {
                                point.setColor("2");//黄色
                                Bitmap bmp = BitmapFactory.decodeResource(res, R.mipmap.monitor_yellow_circle);
                                point.setBitmap(bmp);
                            }
                            if ("0".equals(bean.getIsSpecial())) {
                                if (mContext.getResources().getString(R.string.high_value).equals(pointList.get(i).getName())) {
                                    Bitmap bmp = BitmapFactory.decodeResource(res, R.mipmap.monitor_blue_circle);
                                    point.setBitmap(bmp);
                                } else if (mContext.getResources().getString(R.string.low_value).equals(pointList.get(i).getName())) {
                                    Bitmap bmp = BitmapFactory.decodeResource(res, R.mipmap.monitor_yellow_circle);
                                    point.setBitmap(bmp);
                                }
                            } else {
                                point.setBig(true);
                                Bitmap bmp = BitmapFactory.decodeResource(res, R.mipmap.monitor_red_circle);
                                point.setBitmap(bmp);
                            }
                        } else {
                            if ("0".equals(bean.getIsSpecial())) {
                                Bitmap bmp = BitmapFactory.decodeResource(res, R.mipmap.monitor_purple_circle);
                                point.setBitmap(bmp);
                            } else {
                                point.setBig(true);
                                Bitmap bmp = BitmapFactory.decodeResource(res, R.mipmap.monitor_red_circle);
                                point.setBitmap(bmp);
                            }
                        }
                        values.add(point);
                    }
                    Line line = new Line(values);
                    if ("1".equals(isKindBean)) {
                        line.setColor(mContext.getResources().getColor(R.color.purpli));
                    } else if ("2".equals(isKindBean)) {
                        if (mContext.getResources().getString(R.string.high_value).equals(pointList.get(i).getName())) {
                            line.setColor(mContext.getResources().getColor(R.color.blue));
                        } else if (mContext.getResources().getString(R.string.low_value).equals(pointList.get(i).getName())) {
                            line.setColor(mContext.getResources().getColor(R.color.possible_result_points));
                        }
                    } else {
                        line.setColor(mContext.getResources().getColor(R.color.blue));
                    }

                    lines.add(line);


                }
            }

            data = new LineChartData(lines);

            List<AxisValue> mAxisValues = new ArrayList<AxisValue>();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            String currentTime = format.format(new java.util.Date());
            for (int i = 0; i < 720; i++) {
                String newDate = getDateAdd(30 - (i / 24));
                mAxisValues.add(new AxisValue(i).setLabel(newDate.split("-")[1] + "-" + newDate.split("-")[2]));
//                for (int j = 0; j < 30; j++) {
//                    if (i >= (j * 24) && i < ((j + 1) * 24)) {
//                        if (j % 2 == 0) {
//                            String newDate = getDateAdd(30 - j);
//                            mAxisValues.add(new AxisValue(i).setLabel(newDate.split("-")[1] + "-" + newDate.split("-")[2]));
//                            break;
//                        } else {
//                            mAxisValues.add(new AxisValue(i).setLabel(""));
//                            break;
//                        }
//                    }
//                }
            }

            Axis axisX = new Axis(mAxisValues);

            List<AxisValue> yAxisValues = new ArrayList<AxisValue>();
            int maxInt = (int) Double.parseDouble(max);
            int minInt = (int) Double.parseDouble(min);
//            int interValue = (maxInt - minInt) / 5;
            int interValue = (int) Math.ceil(((double) (maxInt - minInt) / 5));
            yAxisValues.add(new AxisValue(interValue).setLabel(interValue + "")); //为每个对应的i设置相应的label(显示在X轴)
            yAxisValues.add(new AxisValue(interValue * 2).setLabel((interValue * 2) + ""));
            yAxisValues.add(new AxisValue(interValue * 3).setLabel((interValue * 3) + ""));
            yAxisValues.add(new AxisValue(interValue * 4).setLabel((interValue * 4) + ""));
            yAxisValues.add(new AxisValue(interValue * 5).setLabel((interValue * 5) + ""));

            Axis axisY = new Axis(yAxisValues).setHasLines(true);

            data.setAxisXBottom(axisX);
            data.setAxisYLeft(axisY);

            chart.setLineChartData(data);

            final Viewport v = new Viewport(chart.getMaximumViewport());
            v.bottom = 0;
            v.top = (int) Double.parseDouble(max) + maxInt / 10;
            v.left = 0;
            v.right = 750;
            chart.setMaximumViewport(v);
            chart.setCurrentViewport(v);

        }

    }


    public String getDateAdd(int m) {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String now = sdf.format(date);
        long l = date.getTime() - (long) 24 * 60 * 60 * m * 1000;
        String newd = sdf.format(new Date(l));
        return newd;
    }

//    private void setInitPopup() {
//
//        timeTV.setTextColor(mContext.getResources().getColor(R.color.black_text));
//        timeIV.setImageResource(R.mipmap.tab_arrow);
//
//        catTV.setTextColor(mContext.getResources().getColor(R.color.black_text));
//        catIV.setImageResource(R.mipmap.tab_arrow);
//    }

    private void initTimePopup() {
        timePopup = new PopupWindow(this);
        View timeView = LayoutInflater.from(this).inflate(R.layout.popup_layout_timeline, null);
        timePopup.setContentView(timeView);

        timePopup.setWidth(screenWidth);
        timePopup.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        timePopup.setFocusable(true);
        timePopup.setTouchable(true);
        timePopup.setOutsideTouchable(true);
        timePopup.setBackgroundDrawable(new ColorDrawable(0x00000000));


//        dayBtn = (Button) timeView.findViewById(R.id.timeline_btn_day);
        monthBtn = (Button) timeView.findViewById(R.id.timeline_btn_month);
        weekBtn = (Button) timeView.findViewById(R.id.timeline_btn_week);
        timeWholeView = timeView.findViewById(R.id.timeline_view_whole);

//        dayBtn.setOnClickListener(this);
        weekBtn.setOnClickListener(this);
        monthBtn.setOnClickListener(this);

//        dayBtn.setBackground(mContext.getResources().getDrawable(R.drawable.bg_btn_light_blue_corner));
        weekBtn.setBackground(mContext.getResources().getDrawable(R.drawable.bg_btn_light_blue_corner));
        monthBtn.setBackground(mContext.getResources().getDrawable(R.drawable.bg_btn_light_corner));

        timeWholeView.setOnClickListener(this);

        timePopup.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                timeIV.setImageResource(R.mipmap.tab_arrow);
//                BrightnessUtils.lightOn(ChartDetailActivity.this);
            }
        });
    }


    private void initKindPopup() {
        kindPopup = new PopupWindow(this);
        View kindView = LayoutInflater.from(this).inflate(R.layout.popup_layout_kind, null);
        kindPopup.setContentView(kindView);

        kindPopup.setWidth(screenWidth);
        kindPopup.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        kindPopup.setFocusable(true);
        kindPopup.setTouchable(true);
        kindPopup.setOutsideTouchable(true);
        kindPopup.setBackgroundDrawable(new ColorDrawable(0x00000000));

        MonitorKindDao dao = new MonitorKindDao(mContext);
        MonitorKindBean kindBean = dao.getKind(kindId);
        final List<MonitorKindTestBean> testList = new ArrayList<>();
        MonitorKindTestBean bean = new MonitorKindTestBean();
        bean.setIsSelect(true);
        bean.setName("全部");
        bean.setTestId("");
        testList.add(bean);
        testList.addAll(kindBean.getTestList());
//        for (MonitorKindTestBean test : testList) {
//            if (test.getName().contains(xuetang_default) || test.getName().contains(xuezhi_default)) {
//                test.setIsSelect(true);
//                testId = test.getTestId();
//            } else {
//                test.setIsSelect(false);
//            }
//        }

        kindListView = (ListView) kindView.findViewById(R.id.kind_listview);
        kindAdapter = new MonitorKindAdapter(this, testList);
        kindListView.setAdapter(kindAdapter);
        kindListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                setInitPopup();
                kindPopup.dismiss();
                for (int i = 0; i < testList.size(); i++) {
                    if (i == position) {
                        testList.get(i).setIsSelect(true);
                    } else {
                        testList.get(i).setIsSelect(false);
                    }
                }
                catIV.setImageResource(R.mipmap.tab_arrow);
                catTV.setText(testList.get(position).getName());
                kindAdapter.notifyDataSetChanged();
                testId = testList.get(position).getTestId();
                loading();
                YiJiaHuNetWorkUtils.getMonitorData(patientId, periodType, kindId, testId, new Date().getTime() + "", handler);
            }
        });
        kindWholeView = (View) kindView.findViewById(R.id.kind_view_whole);

        kindPopup.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                catIV.setImageResource(R.mipmap.tab_arrow);
//                BrightnessUtils.lightOn(ChartDetailActivity.this);
            }
        });
    }


    public static int daysBetween(String smdate, String bdate) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(sdf.parse(smdate));
        long time1 = cal.getTimeInMillis();
        cal.setTime(sdf.parse(bdate));
        long time2 = cal.getTimeInMillis();
        long between_days = (time2 - time1) / (1000 * 3600 * 24);

        return Integer.parseInt(String.valueOf(between_days));
    }


    @Override
    public void onClick(View v) {
            switch (v.getId()) {
                case R.id.iv_title_back:
                    Intent iBack = new Intent();
                    setResult(RESULT_OK, iBack);
                    finish();
                    break;
                case R.id.tv_title_func:
                    StringUtil.zhugeTrack(mContext, name + "录入保存按钮", name + "录入保存按钮点击数");

                    Intent intentInput = new Intent(mContext, MonitorInputActivity.class);
                    intentInput.putExtra("monitorId", kindId);
                    startActivity(intentInput);
                    break;
                case R.id.xuetangdetail_layout_time:

                    timePopup.showAsDropDown(ivgLayout);
//                    BrightnessUtils.lightOff(ChartDetailActivity.this);
//                    setInitPopup();
                    timeIV.setImageResource(R.mipmap.tab_arrow_up);
                    break;
                case R.id.xuetangdetail_layout_cat:

                    kindPopup.showAsDropDown(ivgLayout);
//                    BrightnessUtils.lightOff(ChartDetailActivity.this);
//                    setInitPopup();
                    catIV.setImageResource(R.mipmap.tab_arrow_up);
                    break;
//                case R.id.timeline_btn_day:
////                    setInitPopup();
//                    timePopup.dismiss();
//                    dayBtn.setBackground(mContext.getResources().getDrawable(R.drawable.bg_btn_light_blue_corner));
//                    weekBtn.setBackground(mContext.getResources().getDrawable(R.drawable.bg_btn_light_corner));
//                    monthBtn.setBackground(mContext.getResources().getDrawable(R.drawable.bg_btn_light_corner));
//                    timeIV.setImageResource(R.mipmap.tab_arrow);
//                    timeTV.setText(mContext.getResources().getString(R.string.timeline_today));
//                    periodType = "0";
//                    YiJiaHuNetWorkUtils.getMonitorData(patientId, periodType, kindId, testId, new Date().getTime() + "", handler);
//                    break;
                case R.id.timeline_btn_week:
//                    setInitPopup();
                    timePopup.dismiss();
//                    dayBtn.setBackground(mContext.getResources().getDrawable(R.drawable.bg_btn_light_corner));
                    weekBtn.setBackground(mContext.getResources().getDrawable(R.drawable.bg_btn_light_blue_corner));
                    monthBtn.setBackground(mContext.getResources().getDrawable(R.drawable.bg_btn_light_corner));
                    timeIV.setImageResource(R.mipmap.tab_arrow);
                    timeTV.setText(mContext.getResources().getString(R.string.timeline_week));
                    periodType = "1";
                    loading();
                    YiJiaHuNetWorkUtils.getMonitorData(patientId, periodType, kindId, testId, new Date().getTime() + "", handler);
                    break;
                case R.id.timeline_btn_month:
//                    setInitPopup();
                    timePopup.dismiss();
//                    dayBtn.setBackground(mContext.getResources().getDrawable(R.drawable.bg_btn_light_corner));
                    weekBtn.setBackground(mContext.getResources().getDrawable(R.drawable.bg_btn_light_corner));
                    monthBtn.setBackground(mContext.getResources().getDrawable(R.drawable.bg_btn_light_blue_corner));
                    timeIV.setImageResource(R.mipmap.tab_arrow);
                    timeTV.setText(mContext.getResources().getString(R.string.timeline_month));
                    periodType = "2";
                    loading();
                    YiJiaHuNetWorkUtils.getMonitorData(patientId, periodType, kindId, testId, new Date().getTime() + "", handler);
                    break;
                case R.id.timeline_view_whole:
//                    setInitPopup();
                    if (timePopup != null) {
                        timePopup.dismiss();
                    }
                    break;
                case R.id.kind_view_whole:
//                    setInitPopup();
                    if (kindPopup != null) {
                        kindPopup.dismiss();
                    }
                    break;
//                case R.id.xueyadetail_tv_day:
//
//                    dayTV.setTextColor(getResources().getColor(R.color.white));
//                    weekTV.setTextColor(getResources().getColor(R.color.light_blue));
//                    monthTV.setTextColor(getResources().getColor(R.color.light_blue));
//                    avgLayout.setBackground(getResources().getDrawable(R.mipmap.blood_nav));
//                    periodType = "0";
//                    YiJiaHuNetWorkUtils.getMonitorData(patientId, periodType, kindId, testId, new Date().getTime() + "", handler);
//
//                    break;

                case R.id.xueyadetail_tv_week:

//                    dayTV.setTextColor(getResources().getColor(R.color.light_blue));
                    weekTV.setTextColor(getResources().getColor(R.color.white));
                    monthTV.setTextColor(getResources().getColor(R.color.light_blue));
//                    avgLayout.setBackground(getResources().getDrawable(R.mipmap.blood_nav_two));
                    avgLayout.setBackgroundResource(R.drawable.blood_nav_left);
                    periodType = "1";
                    loading();
                    YiJiaHuNetWorkUtils.getMonitorData(patientId, periodType, kindId, testId, new Date().getTime() + "", handler);
                    break;
                case R.id.xueyadetail_tv_month:

//                    dayTV.setTextColor(getResources().getColor(R.color.light_blue));
                    weekTV.setTextColor(getResources().getColor(R.color.light_blue));
                    monthTV.setTextColor(getResources().getColor(R.color.white));
//                    avgLayout.setBackground(getResources().getDrawable(R.mipmap.blood_nav_three));
                    avgLayout.setBackgroundResource(R.drawable.blood_nav_right);
                    periodType = "2";
                    loading();
                    YiJiaHuNetWorkUtils.getMonitorData(patientId, periodType, kindId, testId, new Date().getTime() + "", handler);

                    break;
                case R.id.xuetang_settings:
                    Intent intentBlood = new Intent(mContext,AddBloodpressureActivity.class);
                    intentBlood.putExtra("bloodMax",bloodMax);
                    intentBlood.putExtra("bloodMin",bloodMin);
                    intentBlood.putExtra("patientId",patientId);
                    intentBlood.putExtra("axMin",axMin);
                    startActivityForResult(intentBlood,22);
                    break;
                case R.id.ll_linear_blood:
                    Intent intentBloods = new Intent(mContext,AddBloodpressureActivity.class);
                    intentBloods.putExtra("bloodMax",bloodMax);
                    intentBloods.putExtra("bloodMin",bloodMin);
                    intentBloods.putExtra("patientId",patientId);
                    intentBloods.putExtra("axMin",axMin);
                    startActivityForResult(intentBloods,22);
                    break;

                case R.id.iv_title_func:
                    popup_func.showAsDropDown(moreIV, 0, 0);
                    BrightnessUtils.lightOff(ChartDetailActivity.this);
                    break;
                case R.id.tv_chartinfo_func1:
                    popup_func.dismiss();
                    Intent recoveryIntent = new Intent(mContext, BetterPlanEditActivity.class);
                    recoveryIntent.putExtra("patientId",patientId);
                    startActivity(recoveryIntent);
                    break;
                case R.id.tv_chartinfo_func2:
                    popup_func.dismiss();
                    Intent scaleIntent = new Intent(mContext, ScaleQuestionActivity.class);
                    scaleIntent.putExtra("patientId",patientId);
                    scaleIntent.putExtra("isFromAssistant","true");
                    startActivity(scaleIntent);
                    break;
                case R.id.tv_chartinfo_func3:
                    popup_func.dismiss();
                    Intent questionIntent = new Intent(mContext, QuestionListActivity.class);
                    questionIntent.putExtra("patientId",patientId);
                    questionIntent.putExtra("isFromAssistant","true");
                    startActivity(questionIntent);
                    break;
            }

    }

    @Override
    public void PostOptions(View view, int position, String optionType) {
        if ("delete".equals(optionType)) {
            List<String> pointsId = new ArrayList<>();
            pointsId.add(pointList.get(position).getPointId());
            pointsId.add(pointList.get(position).getPointId1());
            pointsId.add(pointList.get(position).getPointId2());
            YiJiaHuNetWorkUtils.getMonitorDelete(pointsId, handler);
        }
        if ("edit".equals(optionType)) {
            Intent intent = new Intent(mContext, MonitorEditActivity.class);
            intent.putExtra("monitorId", kindId);
            intent.putExtra("time", pointList.get(position).getTime());
            intent.putExtra("pointId", pointList.get(position).getPointId());
            intent.putExtra("pointId1", pointList.get(position).getPointId1());
            intent.putExtra("pointId2", pointList.get(position).getPointId2());
            intent.putExtra("value", pointList.get(position).getValue());
            intent.putExtra("value1", pointList.get(position).getValue1());
            intent.putExtra("value2", pointList.get(position).getValue2());
//            mContext.startActivity(intent);
            startActivityForResult(intent, 20);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == 20) {
                loading();
                YiJiaHuNetWorkUtils.getMonitorData(patientId, periodType, kindId, testId, new Date().getTime() + "", handler);
            }
            if (requestCode == 22){
                xuya_high.setText(data.getExtras().getString("highblood"));
                xuya_low.setText(data.getExtras().getString("lowblood"));
                loading();
                YiJiaHuNetWorkUtils.getMonitorData(patientId, periodType, kindId, testId, new Date().getTime() + "", handler);
            }
        }
    }

    private class ValueTouchListener implements LineChartOnValueSelectListener {

        @Override
        public void onValueSelected(int lineIndex, int pointIndex, PointValue value) {
            Toast.makeText(ChartDetailActivity.this, "Selected: " + value, Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onValueDeselected() {
            // TODO Auto-generated method stub

        }

    }

}
